**********************************************************************
*Matching weights 
**********************************************************************
	*File options 
	if $option ==1 {
	local is is
	local profits 
	}
	if $option ==2 {
	local is ss
	local profits 
	}
	*Robustness: match on profits too
	if $option ==3 {
	local is is
	local profits "_profits" 
	}
	if $option ==4 {
	local is ss
	local profits "_profits" 
	}
	
********************************************************************************
*A. Prepare input file 
	*Add 2011 data to regression sample 
	{
	*aha_expenses contains expense variables from the AHA.
	use "$aha\tarball\aha_expenses", clear
	keep id ye exptot old_num
	
	*Lag & inflate expenses
	gegen temp = group(id)
	xtset temp ye 
	
	*Add AHA vars 
	*aha_merge6_1.dta contains hospital-year level data from the AHA survey and 
	*identified mergers, described in Appendix A1, A3. 
	drop if missing(old_num)
	merge 1:1 old_num ye using "$aha\aha_merge6_1", keepusing(num exptot bought_`is' never bdtot np mcare mcaid)
	keep if _m==3
	drop _m old_num id 
	gduplicates drop 
	isid num ye
	
	ds num ye, not 
	local all `r(varlist)'
	foreach var in `all' {
	ren `var' `var'_2
	}
	compress
	tempfile 2011data
	save `2011data', replace
	}
		
	*Matching input file 
	use "$reg\FULLsample`is'", clear
	{
	bysort num: gegen has_incl_1`is' = max(incl_1`is')
	drop if !has_incl_1`is' & !never
	
	*Add 2011 data 
	merge 1:1 num ye using `2011data'
	foreach var in `all' {
	capture noisily replace `var' = `var'_2 if missing(`var') 
	capture noisily ren `var'_2 `var'
	capture noisily drop `var'_2
	}
	bysort num: gegen has_m = max(_m)
	drop if has_m!=3
	drop _m 
	gen mcare_ = mcare + mcaid
	
	*Normalize, lag, impute expenses
	ren bdtot_first bdtot_first2
	bysort num: gegen bdtot_first = max(bdtot_first2)
	drop bdtot_first2
	replace exptot = exptot / bdtot_first 
	xtset num ye
	gen    exptot_l1  = L1.exptot
	replace exptot_l1 = exptot if missing(exptot_l1) & never==1
	
	*Normalize, impute patient margin 
	merge 1:1 num ye using "hcris_cleaned", keepusing(g3*ptnts)
	drop if _m==2
	drop _m
	gen mgn_ptnt = g3_netincome_ptnts / g3_netrev_ptnts
	sum mgn_*, d
	forval i = 2011 / 2018 {
	qui sum mgn if ye==`i', d
	local p95 = r(p95)
	local p5  = r(p5)
	replace mgn = `p95' if mgn > `p95' & !missing(mgn) & ye==`i'
	replace mgn = `p5'  if mgn < `p5'  & !missing(mgn) & ye==`i'
	}
	sum mgn_*, d
	
	xtset num ye
	gen mgn_ptnt_l1     = L1.mgn_ptnt
	replace mgn_ptnt_l1 = mgn_ptnt if missing(mgn_ptnt_l1)
	gen avg_mgn_ptnt    = (mgn_ptnt + mgn_ptnt_l1)/2
	replace avg         = mgn_ptnt_l1 if missing(avg)
	foreach var in avg {
	replace `var' = L1.`var' if missing(`var')
	replace `var' = F1.`var' if missing(`var') & never==1
	replace `var' = F1.`var' if missing(`var') & never==1
	replace `var' = F1.`var' if missing(`var') & never==1
	}	
	keep num ye bought_1`is' bought_`is' pre1_1`is' never bdtot np mcare_ exptot* avg 
	
	*Add bought flag from full data if missing from Elevance sample
	if "`is'"=="ss" {
	replace bought_1ss = 0 if num==390030 & ye==2015
	replace pre        = 1 if num==390030 & ye==2015
	}
	else {
	replace bought_1is = 0 if num==450072 & ye==2015 
	replace pre        = 1 if num==450072 & ye==2015 
	}
	gen yr = year if bought_1`is'==1
	bysort num: gegen boughtyr = min(yr)
	drop yr bought_`is'
	bysort num: gegen haspre = max(pre)
	
	*Drop potential controls with insufficient data
	keep if inrange(ye,2012,2018)
	bysort num: gegen yrct = count(ye)
	drop if never & yrct < 4
	drop yrct	
	qui compress 
	}
	save "$reg\\`is'matchinput", replace
	
********************************************************************************
*B. Coarsened exact matching (CEM)
*1. Match one treatment cohort at a time
	forval b = 2013/2017 {
	use "$reg\\`is'matchinput", clear
	
	*Keep data in year b-1 if bought in b (or a control)
	keep if boughtyr==`b' | never==1
	gen has`b' = 1 if ye==`b'
	bysort num: gegen has`b'2 = max(has`b')
	drop if missing(has`b'2)
	drop has`b'*			
	keep if ye==`b'-1
	
	keep boughtyr num never bdtot np mcare_ exptot *_l? avg 
	gen b = (!missing(boughtyr))
	tab b never, m
	
	*Coarsen vars 
	foreach var in bdtot mcare_ exptot exptot_l1 avg {
	qui sum `var', d
	local `var'1 = r(p25)
	local `var'2 = r(p50)
	local `var'3 = r(p75)
	local `var'max = r(max)
	local `var'min = r(min)
	}

	*Preferred specification
	local match_cond "bdtot(0 `bdtot2' 2000) np mcare_(0 `mcare_2' 1) exptot(`exptotmin' `exptot1' `exptot2' `exptot3' `exptotmax') exptot_l1(`exptot_l1min' `exptot_l11' `exptot_l12' `exptot_l13' `exptot_l1max')"
	if "`profits'"!="_profits" {
	cem `match_cond', tr(b)	
	}
	*Robustness: Matching on profits
	else {
	cem `match_cond' avg(`avgmin' `avg2' `avgmax'), tr(b)	
	}
	
	*gen l1_s = r(L1)
	ren cem_matched cem_matched_s
	ren cem_weights cem_weights_s
	replace cem_strata = . if cem_m==0 
	
	gen year = `b'
	tempfile m`b'
	save `m`b'', replace
	}
	
*2. Consolidate matched cohorts 
	{
	use "$reg\\`is'matchinput", clear
	gen fulldata = 1
	foreach var in cem_matched_s cem_weights_s cem_strata {
	gen `var'_c = .
	}
	capture drop _m 
	forval b = 2013/2017 {
	merge 1:1 num ye using `m`b''	
	foreach var in cem_matched_s cem_weights_s cem_strata {
	replace `var'_c = `var' if _m==3 & !missing(`var')
	drop `var'
	}
	drop _m 
	}
	drop b 
	
	*Never/previously acquired hospitals can serve as controls for treated hospitals in multiple cohorts; consolidate weights 
	bysort num: gegen wt_cemm_s = sum(cem_weights_s_c)
	gegen cem_strata = group(cem_strata year)
	drop cem_w* cem_strata_c
	drop if missing(fulldata)	
	
	keep num ye wt* cem_strata
	label var wt_cemm_s  "cem wts"
	label var cem_strata  "cem strata"
	qui compress 
	save "$reg\fulldata_wts`is'`profits'", replace
	
	*Non-sensitive data (transfer to Medicare server)
	save "$op\fulldata_wts`is'`profits'", replace
	}	